package com.kemai.customize

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.kemai.customize.MultiPointView.MultiPointView.TAG
import com.kemai.view.R
import java.lang.Exception

/**

 *@description:TODO
 * @author: zhaoshanshan
 * @date: 2022/12/2 8:01 下午
 */
class MultiPointView:View {
    private lateinit var bitMap:Bitmap
    private lateinit var paint:Paint
    private var downX = 0.0f
    private var downY = 0.0f
    private var offsetX = 0.0f
    private var offsetY = 0.0f
    private var lastOffsetX = 0.0f
    private var lastOffsetY = 0.0f
    private var currentPointId = 0
    object MultiPointView{
        const val TAG = "MultiPointView"
    }
    constructor(context: Context?) : this(context,null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs,0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes){
        init(context!!)
    }

    private fun init(context:Context) {
        bitMap = BitmapFactory.decodeResource(context.resources, R.mipmap.photo)
        initPaint()
    }
    private fun initPaint(){
        paint = Paint()
        paint.isDither = true
        paint.isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas?) {
        drawBitmap(canvas!!)
    }
    private fun drawBitmap(canvas: Canvas){
        canvas.save()
        canvas.drawBitmap(bitMap,offsetX,offsetY,paint)
        canvas.restore()

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        //when(event!!.action){ 单指事件
         when(event!!.actionMasked){//多指事件
            MotionEvent.ACTION_DOWN->{//第一个手指按下事件
                downX = event.x
                downY = event.y
                currentPointId = 0

            }
            MotionEvent.ACTION_MOVE->{//移动事件
                try {
                    val index = event.findPointerIndex(currentPointId)
                    if(index>=event.pointerCount){
                        return true
                    }
                    offsetX = lastOffsetX+event.getX(index)-downX
                    offsetY = lastOffsetY+event.getY(index)-downY
                    invalidate()
                }catch (e:Exception){
                   e.printStackTrace()
                }
            }
            MotionEvent.ACTION_POINTER_DOWN->{//非第一根手指按下
                val currentIndex = event.actionIndex
                currentPointId = event.getPointerId(currentIndex)
                downX = event.getX(currentIndex)
                downY = event.getY(currentIndex)
                lastOffsetX = offsetX
                lastOffsetY = offsetY

            }
            MotionEvent.ACTION_POINTER_UP->{// 非最后一根手指抬起

                //获取抬起手指的index
                var upIndex = event.actionIndex
                //获取抬起手指的id
                var upPointId = event.findPointerIndex(upIndex)
                if(upPointId==currentPointId){//当前操作等于抬起id 需要做边界越界异常处理
                   if(upIndex==event.pointerCount-1){//最后一根手指
                       upIndex = event.pointerCount-2
                   }else{//非最后一根手指
                       upIndex++

                   }
                    currentPointId = event.findPointerIndex(upIndex)
                    downX = event.getX(upIndex)
                    downY = event.getY(upIndex)
                    lastOffsetX = offsetX
                    lastOffsetY = offsetY
                }


            }
            MotionEvent.ACTION_UP->{//最后一根手指抬起事件
                lastOffsetX = offsetX
                lastOffsetY = offsetY
            }

        }
        return true
    }
}